package com.ruoyi.squadweb.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.squadweb.mapper.SquadjsMapInfoMapper;
import com.ruoyi.squadweb.domain.SquadjsMapInfo;
import com.ruoyi.squadweb.service.ISquadjsMapInfoService;

/**
 * 地图信息Service业务层处理
 *
 * @author LJY
 * @date 2023-10-22
 */

@Service
public class SquadjsMapInfoServiceImpl implements ISquadjsMapInfoService
{
    private static final Logger log = LoggerFactory.getLogger(SquadjsMapInfoServiceImpl.class);
    @Autowired
    private SquadjsMapInfoMapper squadjsMapInfoMapper;

    /**
     * 查询地图信息
     *
     * @param id 地图信息主键
     * @return 地图信息
     */
    @Override
    public SquadjsMapInfo selectSquadjsMapInfoById(Long id)
    {
        return squadjsMapInfoMapper.selectSquadjsMapInfoById(id);
    }

    /**
     * 查询地图信息列表
     *
     * @param squadjsMapInfo 地图信息
     * @return 地图信息
     */
    @Override
    public List<SquadjsMapInfo> selectSquadjsMapInfoList(SquadjsMapInfo squadjsMapInfo)
    {
        return squadjsMapInfoMapper.selectSquadjsMapInfoList(squadjsMapInfo);
    }

    /**
     * 新增地图信息
     *
     * @param squadjsMapInfo 地图信息
     * @return 结果
     */
    @Override
    public int insertSquadjsMapInfo(SquadjsMapInfo squadjsMapInfo)
    {
        return squadjsMapInfoMapper.insertSquadjsMapInfo(squadjsMapInfo);
    }

    /**
     * 修改地图信息
     *
     * @param squadjsMapInfo 地图信息
     * @return 结果
     */
    @Override
    public int updateSquadjsMapInfo(SquadjsMapInfo squadjsMapInfo)
    {
        return squadjsMapInfoMapper.updateSquadjsMapInfo(squadjsMapInfo);
    }

    /**
     * 批量删除地图信息
     *
     * @param ids 需要删除的地图信息主键
     * @return 结果
     */
    @Override
    public int deleteSquadjsMapInfoByIds(Long[] ids)
    {
        return squadjsMapInfoMapper.deleteSquadjsMapInfoByIds(ids);
    }

    /**
     * 删除地图信息信息
     *
     * @param id 地图信息主键
     * @return 结果
     */
    @Override
    public int deleteSquadjsMapInfoById(Long id)
    {
        return squadjsMapInfoMapper.deleteSquadjsMapInfoById(id);
    }

    @Override
    public String importMapInfo(List<SquadjsMapInfo> squadjsMapInfos, boolean updateSupport, String operName) {
        if (StringUtils.isNull(squadjsMapInfos) || squadjsMapInfos.size() == 0)
        {
            throw new ServiceException("导入地图数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SquadjsMapInfo mapInfo : squadjsMapInfos)
        {
            try
            {
                // 验证是否存在这个用户
                SquadjsMapInfo squadjsMapInfo = squadjsMapInfoMapper.selectSquadjsMapInfoById(mapInfo.getId());
                if (StringUtils.isNull(squadjsMapInfo))
                {

                    squadjsMapInfoMapper.insertSquadjsMapInfo(mapInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + mapInfo.getMapNameChinese() + " 导入成功");
                }
                else
                {
                    squadjsMapInfoMapper.updateSquadjsMapInfo(mapInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + mapInfo.getMapNameChinese() + " 更新成功");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + mapInfo.getMapNameChinese() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
